﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CustomException;
using Display.Properties;
using EnglishLibrary;
using DataTransfer;
using Global;

namespace Display
{
    public partial class NoteUC : UserControl
    {
        #region Properties
        //the datatranfer object
        NoteDT noteDT = new NoteDT();
        //the list of Note
        List<Note> _noteList = new List<Note>();
        //the current note
        Note _crrNote = new Note();
        public int Count
        {
            get
            {
                if (this._noteList != null)
                    return this._noteList.Count;
                return 0;
            }
        }
        #endregion

        #region Constructor and Load method
        public NoteUC()
        {
            InitializeComponent();
        }

        private void NoteUC_Load(object sender, EventArgs e)
        {
            //setup the delegate
            this.rateUC1.ChangeRate = new RateUC.SelectedIndexChanged(ChangeRate);
        }
        #endregion

        #region Functional methods
        public void Next()
        {
            if (_noteList.Count <= 0)
                //throw new NoItemException(Resources.Exception_NoItem);
                return;

            //random the index of the note list
            var r = new Random();
            var index = r.Next(0, _noteList.Count);

            //set the current vocabulary
            this._crrNote = _noteList[index];

            //display the item
            this.rtbNote.Text = _noteList[index].NoteText;
            this.rtbExample.Text = _noteList[index].Example;

            //display this current rate
            this.rateUC1.SelectedIndex = Utility.RateIdToNumber(this._crrNote.RateID);
        }   

        internal void ChangeRate(string selectedValue)
        {
            if (this._crrNote.RateID == selectedValue)
            {
                this._crrNote.RateID = selectedValue;
                noteDT.UpdateNote(this._crrNote);
            }
        }

        public void LoadItems()
        {
            //get list of vocabularies from database
            if (Settings.IS_BEFORE_DATE_CHECKED && Settings.BEFORE_DATE != string.Empty)
                _noteList = noteDT.GetItemsBeforeDate(DateTime.Parse(Settings.BEFORE_DATE));
            else if (Settings.IS_AFTER_DATE_CHECKED && Settings.AFTER_DATE != string.Empty)
                _noteList = noteDT.GetItemsAfterDate(DateTime.Parse(Settings.AFTER_DATE));
            else if (Settings.CATEGORY_LIST_DISPLAY.Count > 0)
                _noteList = noteDT.GetItemsByCates(Settings.CATEGORY_LIST_DISPLAY.Keys.ToList());
            else
                _noteList = noteDT.GetListNote(false); //get all the items which are not deleted
        }
        public void DeleteCurrentItem()
        {
            if (_noteList != null && _crrNote != null && _noteList.Contains(_crrNote))
            {
                //delete the current item
                _noteList.Remove(_crrNote);
                //mark deleted in the database
                noteDT.DeleteNote(_crrNote.ItemID);
            }
        }
        #endregion Functional methods
    }
}
